package com.itcast.cn.response;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 重定向
 *
 * @author hyatt
 * @version 1.0
 */
@WebServlet("/response1")
public class ResponseTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setStatus(302);
        response.setHeader("location", "/day13_tomcat/login.html");

        // 简化跳转代码
        // response.sendRedirect("https://www.baidu.com");
        // 优化代码 设置动态的虚拟路径
        // response.sendRedirect("/day13_tomcat/login.html");
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath + "/login.html");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
